﻿using DevExpress.XtraEditors;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.圣同润.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.圣同润
{
    public partial class Form货品档案新增 : DevExpress.XtraEditors.XtraForm
    {
        private String goodsNo { get; set; }
        private List<GoodsDetailDTO> goodsDetails = new List<GoodsDetailDTO>();
        private List<String> productBanner = new List<String>();
        private List<String> productDetail = new List<String>();
        public Form货品档案新增(String className)
        {
            InitializeComponent();
            this.goodsNo = 货品Service.generateGoodsId(className);
            this.textBox货品编号.Text = this.goodsNo;
            this.textBox分类名称.Text = className;
        }

        private void simpleButton保存_Click(object sender, EventArgs e)
        {
            String goodsName = this.textBox货品名称.Text.Trim();
            String className = this.textBox分类名称.Text.Trim();
            String brandName = this.textBox品牌名称.Text.Trim();
            String shortName = this.textBox货品简称.Text.Trim();
            String remark = this.textBox备注.Text.Trim();
            //  货品名称不能为空
            if (String.IsNullOrEmpty(goodsName))
            {
                MessageBox.Show("货品名称不能为空");
                return;
            }
            货品Service.saveProductGoodsValidate(
                this.goodsNo,
                goodsName,
                brandName,
                shortName,
                remark
                );
            MessageBox.Show("保存成功");
            this.Close();
        }

        private void simpleButton新增_Click(object sender, EventArgs e)
        {
            Form货品规格详情 form = new Form货品规格详情(this.goodsNo,this.goodsDetails);
            form.ShowDialog();
            this.goodsDetails = form.goodsDetails;
            this.gridControl货品规格详情.DataSource = this.goodsDetails;
            this.gridView货品规格详情.RefreshData();
        }

        private void simpleButton上传首图_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.goodsNo))
            {
                return;
            }
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
            ofd.Title = "请选择图片文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String url = 货品Service.saveProductBanner(this.goodsNo,true, ofd);
                this.productBanner.Add(url);
                this.refresh首图图片();
            }
        }

        private void simpleButton上传详细图_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.goodsNo))
            {
                return;
            }
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
            ofd.Title = "请选择图片文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String url = 货品Service.saveProductBanner(this.goodsNo, false, ofd);
                this.productDetail.Add(url);
                this.refresh详情图图片();
            }
        }

        private void refresh首图图片()
        {
            //  首图
            List<图片VO> pic首图 = new List<图片VO>();
            foreach (String item in this.productBanner)
            {
                pic首图.Add(new 图片VO(item));
            }
            this.gridControl首图.DataSource = pic首图;
            this.tileView首图.RefreshData();
        }
        private void refresh详情图图片()
        {
            //  详情图
            List<图片VO> pic详情图 = new List<图片VO>();
            foreach (String item in this.productDetail)
            {
                pic详情图.Add(new 图片VO(item));
            }
            this.gridControl详情图.DataSource = pic详情图;
            this.tileView详情图.RefreshData();
        }

        private void simpleButton保存推文_Click(object sender, EventArgs e)
        {
            String slogan = this.richTextBox推文.Text.Trim();
            //  推文不能为空
            if (String.IsNullOrEmpty(slogan))
            {
                MessageBox.Show("推文不能为空");
                return;
            }
            货品Service.saveProductSlogan(
                this.goodsNo,
                slogan
                );
            MessageBox.Show("保存成功");
        }
    }
    public class 图片VO
    {
        public String url { get; set; }
        public Image image { get; set; }
        public 图片VO(String url)
        {
            try
            {
                this.url = ConnectDTO.图片 + url;
                this.image = FileService.GetImageByUrl(this.url);
            }
            catch { 
            }
        }
    }
}